package com.laundry.controller;

import com.laundry.dto.ResultDTO;
import com.laundry.entity.Promotion;
import com.laundry.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/promotion")
public class PromotionController {
    
    @Autowired
    private PromotionService promotionService;
    
    @GetMapping("/list")
    public ResultDTO getPromotions() {
        try {
            return ResultDTO.success(promotionService.findActivePromotions());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取优惠活动列表失败");
        }
    }
    
    @GetMapping("/all")
    public ResultDTO getAllPromotions() {
        try {
            return ResultDTO.success(promotionService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取所有优惠活动失败");
        }
    }
    
    @GetMapping("/detail")
    public ResultDTO getPromotionById(@RequestParam Long id) {
        try {
            return ResultDTO.success(promotionService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取优惠活动详情失败");
        }
    }
    
    @PostMapping("/create")
    public ResultDTO createPromotion(@RequestBody Promotion promotion) {
        try {
            return ResultDTO.success(promotionService.save(promotion));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("创建优惠活动失败");
        }
    }
    
    @PutMapping("/update")
    public ResultDTO updatePromotion(@RequestBody Promotion promotion) {
        try {
            return ResultDTO.success(promotionService.update(promotion));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("更新优惠活动失败");
        }
    }
    
    @DeleteMapping("/delete")
    public ResultDTO deletePromotion(@RequestParam Long id) {
        try {
            promotionService.deleteById(id);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("删除优惠活动失败");
        }
    }
} 